package com.qf.zyh.dao.Impl;

import com.qf.zyh.dao.CartDao;
import com.qf.zyh.entity.Cart;
import com.qf.zyh.entity.Product;
import com.qf.zyh.util.DBUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartDaoImpl implements CartDao {
    QueryRunner qr = new QueryRunner();

    @Override
    public Cart selectcart(Integer pid, int uid) {
        try {
            Map<String, Object> query = qr.query(DBUtil.getConnection(), "select c.cid,c.uid,c.pid,c.ccount,c.cnum,p.tid,p.pname,p.ptime,p.pimage,p.pprice,p.pstate,p.pinfo FROM cart c INNER JOIN product p ON c.pid=p.pid where uid=? and p.pid=?", new MapHandler(), uid, pid);
            if (query!=null) {
                Cart cart = new Cart();
                Product product = new Product();
                BeanUtils.populate(cart,query);
                BeanUtils.populate(product,query);
                cart.setProduct(product);
                return cart;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateCart(int cid, int cnum, double ccount) {
        try {
            return qr.update(DBUtil.getConnection(),"update cart set cnum=?,ccount=? where cid=?",cnum,ccount,cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCart(Cart cart) {
        try {
            return qr.update(DBUtil.getConnection(),"insert into cart values (default,?,?,?,?)",cart.getUid(),cart.getPid(),cart.getCcount(),cart.getCnum());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Cart> getall(int uid) {
        try {
            List<Map<String, Object>> query = qr.query(DBUtil.getConnection(), "select c.cid,c.uid,c.pid,c.ccount,c.cnum,p.tid,p.pname,p.ptime,p.pimage,p.pprice,p.pstate,p.pinfo FROM cart c INNER JOIN product p ON c.pid=p.pid where uid=?", new MapListHandler(), uid);
            List<Cart> ll = new ArrayList<>();
            for (Map<String, Object> map : query){
                Cart cart = new Cart();
                Product pp = new Product();
                BeanUtils.populate(cart,map);
                BeanUtils.populate(pp,map);
                cart.setProduct(pp);
                ll.add(cart);
            }
            return ll;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int daleCart(int uid) {
        try {
            return qr.update(DBUtil.getConnection(),"delete from cart where uid=?",uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleCartid(int uid, Integer pid) {
        try {
            return qr.update(DBUtil.getConnection(),"delete from cart where uid=? and pid=?",uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
